package article_dao

import (
	database "hft/Database"
	"hft/Database/admin"
	"hft/global"
	"hft/test"
	"hft/test/mi/article/arcticle_utils"
	"hft/test/mi/article/article_model"
	"hft/test/mi/tool"
	"strconv"
	"time"
)

func MiS() {
	var list []*admin.LouPan
	global.OC_DB.Find(&list)
	Article(list)
}

func Article(list []*admin.LouPan) {
	var a1 = make([]*article_model.Actie, 0)
	var a2 = make([]*article_model.Actie, 0)
	var a3 = make([]*article_model.Actie, 0)
	test.Sql().Table("flw_actie").Where("sqcode = ?", "442000").Find(&a1)
	test.Sql().Table("flw_actie2").Where("sqcode = ?", "442000").Find(&a2)
	a3 = append(a3, a1...)
	a3 = append(a3, a2...)
	for _, v := range a3 {
		if v.Cate == 2 { //文章
			t, _ := strconv.ParseInt(v.AddTime, 10, 64)
			formatTime := time.Unix(t, 0)
			cont := tool.ReplaceString(tool.ReplaceString(tool.ProcessImgTags(v.Cont), "amp;", ""), "div", "p")
			var haxVideo bool = false
			if tool.CheckTags(cont) == 2 {
				haxVideo = true
			}
			var pureText, _ = tool.ConvertHTMLToPlainText(cont)
			pureText = tool.TruncateString(pureText, 200)
			var item = &admin.ArticleBase{
				Base:        database.Base{Sort: 1, CreateTime: formatTime, UpdateTime: formatTime},
				Name:        v.Title,
				Author:      v.Lpname,
				Cover:       v.Fpic,
				ArtType:     3,
				AccountId:   152,
				AccountLink: ",1,151,152,",
				RegionLink:  ",1,3,72,",
				HasVideo:    haxVideo,
				PureText:    pureText,
				Cont:        &admin.ArticleCont{Content: cont, Base: database.Base{CreateTime: formatTime, UpdateTime: formatTime}},
			}
			// articleList = append(articleList, item)
			global.OC_DB.Create(&item)
		} else {
			lou := arcticle_utils.FindLou(list, v.Lpname)
			var regionLink string
			var louId int
			if lou != nil {
				t, _ := strconv.ParseInt(v.AddTime, 10, 64)
				formatTime := time.Unix(t, 0)
				cont := tool.ReplaceString(tool.ReplaceString(tool.ProcessImgTags(v.Cont), "amp;", ""), "div", "p")
				var haxVideo bool = false
				if tool.CheckTags(cont) == 2 {
					haxVideo = true
				}
				var pureText, _ = tool.ConvertHTMLToPlainText(cont)
				pureText = tool.TruncateString(pureText, 200)
				regionLink = lou.RegionLink
				louId = int(lou.ID)
				var item = &admin.ActiveBase{
					Base:        database.Base{CreateTime: formatTime, UpdateTime: formatTime},
					AccountId:   152,
					AccountLink: ",1,151,152,",
					Name:        v.Title,
					Cover:       v.Fpic,
					HasVideo:    haxVideo,
					ActiveGroup: arcticle_utils.GetActiveType(v.Type),
					PureText:    pureText,
					Cont:        &admin.ActiveCont{Content: cont, Base: database.Base{CreateTime: formatTime, UpdateTime: formatTime}},
					LouPanId:    louId,
					RegionLink:  regionLink,
				}
				global.OC_DB.Create(&item)
			}
		}
	}

}
